home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 20 / Cream of the Crop 20 (Terry Blount) (1996).iso / utility / freedos.zip / COM050.ZIP / BATCH.C < prev    next >
C/C++ Source or Header  |  1996-01-17  |  7KB  |  315 lines

  1. /* 
  2.  *  BATCH.C - batch file processor for COMMAND.COM.
  3.  *
  4.  *
  5.  *
  6.  *  Comments:
  7.  *
  8.  *  ??/??/?? (Evan Jeffrey) -------------------------------------------------
  9.  *    started.
  10.  *  
  11.  *  07/15/95 (Tim Norman) ---------------------------------------------------
  12.  *    modes and bugfixes.
  13.  *
  14.  *  08/08/95 (Matt Rains) ---------------------------------------------------
  15.  *    i have cleaned up the source code. changes now bring this source into
  16.  *    guidelines for recommended programming practice.
  17.  *
  18.  *    i have added some constants to help making changes easier.
  19.  */
  20.  
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <ctype.h>
  24. #include <string.h>
  25. #include <conio.h>
  26.  
  27. #include "command.h"
  28.  
  29. #define D_LABELERR   "ERROR: label not found!"
  30. #define D_SYNTAXERR  "ERROR: syntax error!"
  31. #define D_PAUSEMSG   "[press any key to continue]"
  32. #define D_BEEP       "beep"
  33. #define D_CALL       "call"
  34. #define D_DOWN       "down"
  35. #define D_ECHO       "echo"
  36. #define D_ERRORLEVEL "errorlevel"
  37. #define D_EXISTS     "exists"
  38. #define D_GOTO       "goto"
  39. #define D_IF         "if"
  40. #define D_NOT        "not"
  41. #define D_ON         "on"
  42. #define D_OFF        "off"
  43. #define D_PAUSE      "pause"
  44. #define D_REM        "rem"
  45. #define D_SHIFT      "shift"
  46.  
  47. /*
  48.  *
  49.  *
  50.  *
  51.  */
  52. int batch(char *fullpath, int argc, char *argv[])
  53. {
  54.    FILE *bfile;
  55.  
  56.    static int nestlevel = 0; 
  57.    static int called;
  58.    static int echo = 1;
  59.    int count; 
  60.    int tokens;
  61.    int shiftlevel = 0;
  62.    int offset = 0;
  63.    int linecho = 1; 
  64.    int charnum;
  65.    char textline[1024];
  66.    char cmdline[1024]; 
  67.    char *p[128];
  68.    
  69.    /* varibles found within code */
  70.    int notval = 0;
  71.    int found = 0;
  72.    char varname[64];
  73.    char filestring[256];
  74.  
  75.    nestlevel++; /* keep track of how many batch files are running */
  76.    printf("\n");
  77.  
  78.    if(!(bfile = fopen(fullpath, "rt")))
  79.    {
  80.       return(1);
  81.    }
  82.    
  83.    called = 1; /* used to determine if a batch file has been nested without the CALL comand */
  84.    
  85.    while(fgets(textline, 1023, bfile))
  86.    {
  87.       if(!strip(textline))
  88.       {
  89.          puts(D_SYNTAXERR);
  90.          return(0);
  91.       }
  92.       if(textline[strlen(textline) - 2] == ' ' && textline[strlen(textline) - 1] == '\\')
  93.       {
  94.          fgets(textline+strlen(textline) - 1, 1023, bfile);
  95.          
  96.          if(!strip(textline))
  97.          {
  98.             puts(D_SYNTAXERR);
  99.             return(0);
  100.          }
  101.       }
  102.       tokens = split(textline, p);
  103.  
  104.       for(count = 0, offset = 0; count < tokens; count++)
  105.       {
  106.          charnum = 0;
  107.  
  108.          while(p[count][charnum])
  109.          {
  110.             if(p[count][charnum] == '%')
  111.             {
  112.                if(isdigit(p[count][charnum+1]))
  113.                {
  114.                   if(p[count][charnum+1] - '0' < argc - shiftlevel)
  115.                   {
  116.                      strcpy(&cmdline[offset], argv[p[count][charnum + 1] + shiftlevel - '0']);
  117.                      offset += strlen(argv[p[count][charnum+1] + shiftlevel - '0']) + 1;
  118.                      cmdline[offset - 1] = ' ';
  119.                   }
  120.                   
  121.                   charnum++;
  122.                }
  123.                else if(p[count][charnum + 1] == '%')
  124.                {
  125.                   cmdline[offset] = '%';
  126.                   offset++;
  127.                   charnum += 2;
  128.                }
  129.                else if(strchr(p[count] + charnum + 1, '%'))
  130.                {
  131.                   strcpy(varname, p[count] + charnum + 1);
  132.                   *strchr(varname, '%') = 0;
  133.                   
  134.                   strupr(varname); /* env. var must be upper case */
  135.  
  136.                   if(getenv(varname))
  137.                   {
  138.                      strcpy(&cmdline[offset], getenv(varname));
  139.                      offset += strlen(getenv(varname));
  140.                   }
  141.  
  142.                   charnum+=strlen(varname)+2;
  143.                }
  144.                else /* add code for environmental variables, strip 1 % sign from FOR variables */
  145.                {
  146.                   ; /* ignore the % sign, print the rest */
  147.                }
  148.             }
  149.             else
  150.             {
  151.                cmdline[offset] = p[count][charnum];
  152.                offset++;
  153.             }
  154.             
  155.             charnum++;
  156.          }
  157.          
  158.          cmdline[offset] = ' ';
  159.          offset++;
  160.       }
  161.       
  162.       cmdline[offset - 1] = 0;
  163.       strcpy(textline, cmdline);
  164.       tokens = split(textline,p);
  165.       
  166.       if(textline[0] == '@')
  167.       {
  168.          linecho = 0;
  169.          strcpy(cmdline, cmdline + 1);
  170.          p[0]++;
  171.       }
  172.       if(linecho && echo)
  173.       {
  174.          printprompt();
  175.          puts(cmdline);
  176.       }
  177.       if(p[0][0] == 0)
  178.       {
  179.          ;
  180.       }
  181.       else if(!strcmpi(p[0], D_SHIFT))
  182.       {
  183.          if(!strcmpi(p[1], D_DOWN))
  184.          {
  185.             shiftlevel--;
  186.             
  187.             if(shiftlevel < 0)
  188.             {
  189.                shiftlevel = 0;
  190.             }
  191.          }
  192.          else
  193.          {
  194.             shiftlevel++;
  195.          }
  196.       }
  197.       else if(!strcmpi(p[0], D_PAUSE))
  198.       {
  199.          puts(D_PAUSEMSG);
  200.          _getch();
  201.       }                      
  202.       else if(!strcmpi(p[0], D_ECHO))
  203.       {
  204.          if(!strcmpi(p[1], D_OFF))
  205.          {
  206.             echo = 0;
  207.          }
  208.          else if(!strcmpi(p[1], D_ON))
  209.          {
  210.             echo = 1;
  211.          }
  212.          else
  213.          {
  214.             puts(cmdline + 5);
  215.          }
  216.       }
  217.       else if(!strcmpi(p[0], D_GOTO))
  218.       {
  219.          rewind(bfile);
  220.          
  221.          while((found - 1) && fgets(filestring, 255, bfile))
  222.          {
  223.             filestring[strlen(filestring) - 1] = 0;
  224.             *strchr(filestring,' ') = 0;
  225.             
  226.             if(filestring[0] == ':')
  227.             {
  228.                if(!strcmpi(filestring + 1, p[1]))
  229.                {
  230.                   found = 1;
  231.                }
  232.             }
  233.          }
  234.          
  235.          if(!found)
  236.          {
  237.             puts(D_LABELERR);
  238.             return(1);
  239.          }
  240.       }
  241.       else if(!strcmpi(p[0], D_IF))
  242.       {
  243.          if(!strcmpi(p[1], D_NOT))
  244.          {
  245.             notval++;
  246.          }
  247.          if(!strcmpi(p[1 + notval], D_ERRORLEVEL))
  248.          {
  249.             ;
  250.          }
  251.          else if(!strcmpi(p[1 + notval], D_EXISTS))
  252.          {
  253.             ;
  254.          }
  255.          else
  256.          {
  257.             ;
  258.          }
  259.       }
  260.       else if(!strcmpi(p[0], D_REM))
  261.       {
  262.          ;
  263.       }
  264.       else if(!strcmpi(p[0], D_BEEP))
  265.       {
  266.          printf("\a");
  267.       }
  268.       else if(!strcmpi(p[0], D_CALL))
  269.       {
  270.          parsecommandline(cmdline + 5);
  271.       }
  272.       else if(p[0][0] == ':')
  273.       {
  274.          ;
  275.       }
  276.       else
  277.       {
  278.          called = 0;
  279.          parsecommandline(cmdline);
  280.          
  281.          if(called)
  282.          {
  283.             return(0);
  284.          }
  285.          
  286.          called = 1;
  287.       }
  288.    }
  289.    
  290.    fclose(bfile);
  291.    nestlevel--;
  292.    
  293.    if(argc)
  294.    {
  295.       ;
  296.    }
  297.    
  298.    if(nestlevel == 0)
  299.    {
  300.       echo = 1;
  301.    }
  302.  
  303.    return(0);
  304. }
  305.  
  306. /*
  307.  * comment this out if you want to use MickeySoft C
  308.  *
  309.  *
  310.  */
  311. int _getch()
  312. {
  313.    return(getch());
  314. }
  315.